//
//  LeftMenuController.m
//  oschina_song
//
//  Created by song on 16/1/25.
//  Copyright © 2016年 song. All rights reserved.
//

#import "LeftMenuController.h"
#import "OSCUser.h"
#import "Config.h"
#import "UIView+Utils.h"
#import "UIImageView+Util.h"
#import "RESideMenu.h"
#import "MeViewController.h"
#import "SettingsPage.h"

@interface LeftMenuController ()

@end

@implementation LeftMenuController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.separatorStyle  =UITableViewCellSelectionStyleNone;
    self.tableView.tableFooterView =[[UIView alloc] initWithFrame:CGRectZero];
    self.tableView.bounces = NO;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    
    return 5;

}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 160;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    OSCUser *myProfile = [Config myProfile];
    
    UIView *headerView =[UIView new];
    UIImageView *avatar =[UIImageView new];
    avatar.contentMode =UIViewContentModeScaleAspectFill;
    avatar.userInteractionEnabled = YES;
    avatar.translatesAutoresizingMaskIntoConstraints = NO;
    [avatar setCornerRadius:30];
    [avatar loadProtrain:myProfile.portraitURL];
    [headerView addSubview:avatar];
    
    UILabel *nameLabel = [UILabel new];
    nameLabel.text = myProfile.name;
    nameLabel.font = [UIFont boldSystemFontOfSize:20];
    nameLabel.translatesAutoresizingMaskIntoConstraints = NO;
    [headerView addSubview:nameLabel];
    NSDictionary *views = NSDictionaryOfVariableBindings(avatar, nameLabel);
    NSDictionary *metrics = @{@"x": @([UIScreen mainScreen].bounds.size.width / 4 - 15)};
    [headerView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[avatar(60)]-10-[nameLabel]-15-|" options:NSLayoutFormatAlignAllCenterX metrics:nil views:views]];
    [headerView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|-x-[avatar(60)]" options:0 metrics:metrics views:views]];
    
    
    return headerView;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell *cell = [UITableViewCell new];
    cell.imageView.image = [UIImage imageNamed:@[@"sidemenu_QA", @"sidemenu-software", @"sidemenu_blog", @"sidemenu_setting", @"sidemenu-night"][indexPath.row]];
    cell.textLabel.text = @[@"技术问答", @"开源软件", @"博客区", @"设置", @"夜间模式", @"注销"][indexPath.row];
    
    return cell;
}

-(void)setContentViewController:(UIViewController *)viewController
{
    viewController.hidesBottomBarWhenPushed = YES;
    UINavigationController *nav =((UITabBarController *)(self.sideMenuViewController.contentViewController)).selectedViewController;
    [nav pushViewController:viewController animated:YES];
    [self.sideMenuViewController hideMenuViewController];
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    switch (indexPath.row) {
        case 0:
        {
            UIViewController *vc = [[UIViewController alloc] init];
            vc.view.backgroundColor =[ UIColor redColor];
            [self setContentViewController:vc];
            break;
        }
        case 1:
        {
            break;
        }
        case 2:{
            break;
        }
        case 3:{
            SettingsPage *page = [SettingsPage new];
            [self setContentViewController:page];
            break;
        }
        case 4:{
            
        }
        default:
            break;
    }
}

@end
